import { Core } from './core';
import Shape from './core.shape';

export class Group extends Core.Element {

  children: Array<Core.Element> = [];

  constructor(config: Partial<Core.IConfig> = {}) {
    super(config);
  }

  addShape(shape: Shape) {
    shape.props.parent = this;
    this.children.push(shape);
  }

  addGroup(group: Group = new Group()) {
    group.props.parent = this;
    this.children.push(group);
  }

  sort() {
    this.children.sort((a, b) => {
      return a.getProps('zIndex') - b.getProps('zIndex');
    });
  }

  drawInner() {
    if (this.getProps('destroyed')) {
      return;
    }
    if (this.getProps('visible')) {
      this.children.forEach((item: Group | Shape) => {
        item.setContext(this.getProps('context'));
        item.drawInner();
      });
    }
  }
}